package org.mineground.handlers;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.bukkit.configuration.file.FileConfiguration;
import org.mineground.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @file ConfigHandler.java (22.01.2012)
 * @author Daniel Koenen
 *
 */
public class ConfigHandler {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(ConfigHandler.class);
    
    private FileConfiguration configHandler;
    
    public boolean liveVersion;

    public String ircBotName;
    public String ircNetwork;
    public String ircBindAddr;
    public String ircChannel;
    public String ircDevChannel;
    public String ircDevPassword;
    public char ircCommandPrefix;
    public int ircPort;

    public String mySQLHost;
    public String mySQLUser;
    public String mySQLPassword;
    public String mySQLDatabase;
    public int mySQLPort;

    public String adminChatPrefix;
    public String groupChatPrefix;
    
    public String mainWorldName;
    public String survivalWorldName;
    public String survivalNetherWorldName;
    public String creativeWorldName;
    public String classicWorldName;

    public List<String> reactionBlockName = new ArrayList<String>();
    public List<Integer> reactionBlockId = new ArrayList<Integer>();
    public List<Integer> reactionBlockAmount = new ArrayList<Integer>();
    
    public int reactionTestDelay;
    public int announceMessageDelay;
    public int timeDelay;
    
    public ConfigHandler(FileConfiguration configFile) {
        configHandler = configFile;
        readConfig();
    }
    
    private void readConfig() {
        liveVersion = configHandler.getBoolean("Options.liveVersion", false);
	adminChatPrefix = configHandler.getString("Options.adminChatPrefix", "@");
	groupChatPrefix = configHandler.getString("Options.groupChatPrefix", "#");
        
        ircBotName = configHandler.getString("IRC.botName", "Gunther");
	ircNetwork = configHandler.getString("IRC.network", "irc.gtanet.com");
	ircBindAddr = configHandler.getString("IRC.bindAddress", "127.0.0.1");
	ircChannel = (liveVersion) ? (configHandler.getString("IRC.echoChannel", "#LVP.Minecraft")) : (configHandler.getString("IRC.testChannel", "#LVP.Minecraft2"));
	ircDevChannel = configHandler.getString("IRC.devChannel", "#LVP.Minecraft.Dev");
	ircDevPassword = configHandler.getString("IRC.devPassword", "bla");
	ircCommandPrefix = configHandler.getString("IRC.commandPrefix", "!").charAt(0);
	ircPort = configHandler.getInt("IRC.port", 6667);

	mySQLHost = configHandler.getString("MySQL.host", "localhost");
	mySQLUser = configHandler.getString("MySQL.user", "root");
	mySQLPassword = configHandler.getString("MySQL.password", "");
	mySQLDatabase = configHandler.getString("MySQL.database", "mineground");
	mySQLPort = configHandler.getInt("MySQL.port", 3306);
        
        mainWorldName = configHandler.getString("World.mainName", "main");
        survivalWorldName = configHandler.getString("World.survivalName", "survival");
        survivalNetherWorldName = configHandler.getString("World.survivalNetherName", "survival_nether");
        creativeWorldName = configHandler.getString("World.creativeName", "creative");
        classicWorldName = configHandler.getString("World.classicName", "classic");
        
        List<String> reactionPrice = configHandler.getStringList("Reactiontest.Prices");
	Iterator<String> iterator;

	iterator = reactionPrice.iterator();
	while (iterator.hasNext()) {
	    String[] values = iterator.next().toString().split(",");

            reactionBlockName.add(values[0]);
            reactionBlockId.add(Integer.parseInt(values[1]));
            reactionBlockAmount.add(Integer.parseInt(values[2]));
	}

	reactionTestDelay = configHandler.getInt("Reactiontest.delay", 5);
        announceMessageDelay = configHandler.getInt("AnnounceMessage.delay", 2);
        timeDelay = configHandler.getInt("Time.delay", 10);
    }
    
    public boolean writeDefaultConfig() {
        configHandler.set("Options.liveVersion", false);
        configHandler.set("Options.adminChatPrefix", "@");
        configHandler.set("Options.groupChatPrefix", "#");
        
        configHandler.set("IRC.botName", "Gunther");
	configHandler.set("IRC.network", "irc.gtanet.com");
	configHandler.set("IRC.bindAddress", "127.0.0.1");
	configHandler.set("IRC.testChannel", "#LVP.Minecraft2");
	configHandler.set("IRC.devChannel", "#LVP.Minecraft.Dev");
	configHandler.set("IRC.devPassword", "bla");
	configHandler.set("IRC.commandPrefix", "!");
	configHandler.set("IRC.port", 6667);
        
        configHandler.set("MySQL.host", "localhost");
	configHandler.set("MySQL.user", "root");
	configHandler.set("MySQL.password", "");
	configHandler.set("MySQL.database", "mineground");
	configHandler.set("MySQL.port", 3306);
        
        configHandler.set("World.mainName", "main");
        configHandler.set("World.survivalName", "survival");
        configHandler.set("World.creativeName", "creative");
        configHandler.set("World.oldName", "old");
        
        configHandler.set("Reactiontest.delay", 5);
        configHandler.set("Reactiontest.Prices", "Format: blockname,blockid,amountofblocks");
        
        try {
            configHandler.save(new File(Main.getInstance().getDataFolder(), "config.yml"));
            return true;
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
            return false;
        }
    }
}
